<?php

$action = getAction();

switch ($action) {
	case "index":
		$promoter = trim(htmlspecialchars($_GET["promoter"]));
		
		$user = $db->getRow("select * from " . table("user") . " where openid = '$openid'");
		$flashList = $db->getAll("select * from " . table("flash") . " where status = '1' order by seq_no asc");
		
		$smarty->assign("promoter", $promoter);
		$smarty->assign("user", $user);
		$smarty->assign("flashList", $flashList);
		$smarty->display("recharge.html");
		break;
		
	case "getProduct":
		$mobile = trim($_GET["mobile"]);
		
		$result = array();
		if(!$mobile) {
			$result["success"] = false;
			
			echo json_encode($result);
			exit();
		}
		
		$result["success"] = true;

		//读取手机号码地域信息
		$url = "http://122.224.212.160:8980/common/area.action?num=$mobile";
		$mobileData = file_get_contents($url);
		$mobileData = json_decode($mobileData, true);
		$result["mobileData"] = $mobileData;
		
		$provinceCode = $mobileData["data"]["provinceCode"];
		$opCode = $mobileData["data"]["opCode"];
		
		if($provinceCode != '33' || $opCode != "0" ) {
			$provinceCode = "0";
		}
		//判断后台开启的是什么接口
		//查询后台配置的流量接口  1.麦流量  2.速迅
		$setting = $db->getRow("select liuliang from " . table("setting"));
		if($setting['liuliang']=='1'){
		    $sql = " select * from " . table("product")
		    . " where type=1 and status = '" . STATUS::VALID . "'"
		        . " and mobile_type = '$opCode' "
		        . " and province_code = '$provinceCode' "
		        . " order by mb_value asc ";
		}elseif($setting['liuliang']=='2'){
		    $sql = " select * from " . table("product")
		    . " where type=2 and status = '" . STATUS::VALID . "'"
		        . " and mobile_type = '$opCode' "
		        . " and province_code = '$provinceCode' "
		        . " order by mb_value asc ";
		}
		

		$productList = $db->getAll($sql);
		
		for($i = 0; $i < count($productList); $i++) {
			$productList[$i]["range_name"] = USE_RANGE::getRangeName($productList[$i]["use_range"]);
		}
		$result["products"] = $productList;
		
		echo json_encode($result);
		exit();
		break;
	case "pay":
		$prodId = trim($_GET["id"]);
		$mobile = trim($_GET["mobile"]);
		$mobileType = trim($_GET["scope"]);
		$promoter = trim($_GET["promoter"]);
		$orderId = "";
		
		$product = $db->getRow("select * from " . table("product") . " where id = '$prodId' and status = '" . STATUS::VALID . "'");
		
		if(!$product || !$mobile || !$mobileType) {
			errback("无效产品，或者无效手机号码");
		}
		
		if($product && $mobile && $mobileType) {
			$order["openid"] = $openid;
			$order["product_id"] = $prodId;
			$order["promoter_id"] = $promoter;
			$order["phone"] = $mobile;
			$order["out_trade_no"] = rand(100000, 999999) . time() . rand(100000, 999999);;
			$order["mobile_type"] = $mobileType;
			$order["mb_value"] = $product["mb_value"];
			$order["use_range"] = $product["use_range"];
			$order["valid_type1"] = $product["valid_type1"];
			$order["valid_type2"] = $product["valid_type2"];
			$order["limit_times"] = $product["limit_times"];
			$order["support_user"] = $product["support_user"];
			$order["bonues_price"] = $product["bonues_price"];
			$order["status"] = ORDER_STATUS::NEW_ORDER;
			$order["insert_time"] = date("Y-m-d H:i:s");
			$order["update_time"] = date("Y-m-d H:i:s");
			
			$orderId = $db->insert("order", $order);
		}
		header("location: pay/payDetail.php?orderId=$orderId");
		exit();
		break;
}
?>